package com.lanou.cn.springboot.demo.controller;

import com.lanou.cn.springboot.demo.mapper.ProvinceMapper;
import com.lanou.cn.springboot.demo.mapper.UserMapper;
import com.lanou.cn.springboot.demo.pojo.City;
import com.lanou.cn.springboot.demo.pojo.Province;
import com.lanou.cn.springboot.demo.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: GaoXiaoPeng
 * @Description:
 * @Date: Created in 上午11:01 2018/2/27
 */
@Controller
@RequestMapping("/")
public class SpringbootController {


//使用JdbcTemplate类
//    @Resource
//   // private DataSource datasource;
//    private JdbcTemplate jdbcTemplate;
//    @RequestMapping("getDB")
//    public ModelAndView selectDB(String username,ModelAndView mav){
//        User user=jdbcTemplate.queryForObject("select * from test where name=?",new BeanPropertyRowMapper<>(User.class),username);
//        mav.addObject("user",user);
//        mav.setViewName("test");
//        System.out.print(user);
//        System.out.println("111");
//        return mav;
//
//    }
    @RequestMapping("")
    public String indexHtml(){


        return "hello";
    }

    @Resource
    // private DataSource datasource;
    private UserMapper useMapper;
    @RequestMapping("getDB2")
    public ModelAndView selectDB2(String username,ModelAndView mav){
        User user=useMapper.findUserByName(username);
//        User user=jdbcTemplate.queryForObject("select * from test where name=?",new BeanPropertyRowMapper<>(User.class),username);
        mav.addObject("user",user);
        mav.setViewName("test");
        System.out.print(user);
//        System.out.println("111");
        return mav;

    }


     @RequestMapping("hello")
     public String work(){


         return "test";
     }
    @RequestMapping("hello2")
    public ModelAndView work2(@RequestParam("username") String username, ModelAndView mav){
       // mav.addObject(username);
        mav.addObject("username",username);
        //mav.setViewName("test");
        ArrayList al=new ArrayList();
        al.add("zs1");
        al.add("zs2");
        al.add("zs3");
        al.add("zs4");
        mav.addObject("aaa",al);
        try {
           String ipaddress= InetAddress.getLocalHost().getHostAddress();
            System.out.printf(ipaddress);
           System.out.print(ipaddress);
            System.out.println(222);
            System.out.println(222);
            System.out.println(ipaddress);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println("3333");
        mav.setViewName("test");

        return mav;
    }
    @Resource
    private ProvinceMapper pm;
    @RequestMapping("province")
    @ResponseBody
    public List<City> ajaxString(@RequestBody Province p){
        System.out.println(p.getId());
        List<City> lc=pm.findCityByProvince(p);
        for(City  c:lc){
            System.out.println(c);
        }
        System.out.println("----");
        City c=new City();
        c.setId(5);
        c.setCname("111");
        c.setPid(p.getId());
        System.out.println("44669990000");
        System.out.println("44669990000222");
        return lc;
    }
    @RequestMapping("province2")
    public ModelAndView ajaxString2(@RequestBody Province p,ModelAndView mav){
        System.out.println(p.getId());
        List<City> lc=pm.findCityByProvince(p);
        for(City  c:lc){
            System.out.println(c);
        }
        mav.addObject("lcity",lc);
        mav.setViewName("test");
        return mav;
    }

    @RequestMapping("province3")
    public ModelAndView ajaxString3(@RequestBody Province p,ModelAndView mav){
        System.out.println(p.getId());
        List<City> lc=pm.findCityByProvince(p);
        for(City  c:lc){
            System.out.println(c);
        }
        mav.addObject("lcity",lc);
        mav.setViewName("test");
        return mav;
    }
}
